package com.wm.task.view;

import com.wm.task.dao.MarketDAO;
import com.wm.task.vo.User;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class MainView extends BaseView {
    private User user;
    private MarketDAO marketDAO = new MarketDAO();
    private MainViewII mainViewII;

    @Override
    protected void handleChoice(String choice) {
        switch (choice) {
            case "1"://注册
                this.register();
                break;
            case "2"://登录
                this.login();
                break;
        }

    }

    private void login() {
        System.out.println("====登录===");
        System.out.print("请输入邮箱:");
        String email = sc.next();
        System.out.print("请输入密码:");
        String pass = sc.next();
        user = marketDAO.login(email, pass);
        if (user != null) {
            mainViewII = new MainViewII(user);
            mainViewII.show();

        } else {
            System.out.println("账号或密码错误");
        }

    }

    private void register() {
        System.out.println("===注册===");
        System.out.print("请输入邮箱:");
        String email = sc.next();
        String pass;
        while (true) {
            System.out.print("请输入密码:");
            pass = sc.next();
            System.out.print("请再次输入密码:");
            if (pass.equals(sc.next())) {
                break;
            } else {
                System.out.println("两次密码不一致,请重新输入");
            }
        }
        System.out.print("请输入真实姓名:");
        String name = sc.next();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dddd HH:mm:ss");
        Date now = new Date();
        String format = simpleDateFormat.format(now);
        System.out.println(format);
        user = new User(email, pass, name, format);
        if (marketDAO.register(user)) {
            System.out.println("注册成功");
        } else {
            System.out.println("该邮箱已被注册");
        }
    }

    @Override
    protected String getTitle() {
        return "万码商城";
    }

    @Override
    protected String[] getMenus() {
        String[] menus = {"注册", "登录", "结束"};
        return menus;
    }
}
